<?php
class Astudio {
	public static function main($funcode) {
		sview::verify();
		switch ($funcode) {
			case 1:
				self::__dolistbyids();
				break;
			case 2:
				self::__dosubscribe();
				break;
			case 3:
				self::__dovisitor();
				break;
			default:
				sview::$view->assign_error(404, lang::text(404));
		}
	}
	
	//我的历史访问4-2-1
	private static function __dolistbyids() {
		$studioids = sview::$ctrl['studioids'];
		$studioids = explode(',', $studioids);
		if ($studioids) {
			$sorm  = model::load('live/studio');
			$uorm  = model::load('live/userinfo');
			$aorm  = model::load('live/anchor');
			$list  = array();
			$feed  = array();
			if ($acl->user) {
				$feed   = api_studio::feed($acl->user['id']);
			}
			foreach ($studioids as $studioid) {
				$studio = $sorm->get_one($studioid);
				if (!$studio || !$studio['status']) {
					continue;
				}
				$studio['tags'] = empty($studio['tags'])? array():json_decode($studio['tags'], true);
				$anchor = $aorm->get_one($studio['userid']);
				$user   = $uorm->set_devision($studio['userid'])->get_one($studio['userid']);
				$lv     = api_lv::item($user['lv'], 0);
				$isfeed = in_array($studioid, $feed)? 1:0;
				$iscurated = api_studio::grade($anchor['starlv'], $studio['iscurated']);
				$user['nickname'] = htmlspecialchars_decode($user['nickname']);
				$list[] = array('userid'=>$studio['userid'], 'studioid'=>$studio['id'], 'nickname'=>$user['nickname'], 
					'avtar'=>$user['thumb'], 'lvicon'=>$lv['iconsrc'], 'title'=>$studio['title'], 'thumb'=>$studio['thumb'],
					'viewer'=>$studio['viewer'], 'olstatus'=>$studio['olstatus'], 'airtime'=>$studio['airtime'], 'isfeed'=>$isfeed, 
					'neartime'=>$studio['neartime'], 'alv'=>$user['lv'], 'tags'=>$studio['tags'], 'iscurated'=>$iscurated);
			}
		}
		sview::$view->assign('datasets', $list);
		sview::$view->display();
	}
	
	//我的预约 4-2-2
	private static function __dosubscribe() {
		$acl  = acl::getinst('vedio');
		$list = array();
		if ($acl->user) {
			$userid = $acl->user['id'];
			$feed   = api_studio::feed($userid);
			if ($feed) {
				$sorm  = model::load('live/studio');
				$uorm  = model::load('live/userinfo');
				$aorm  = model::load('live/anchor');
				$list  = array();
				foreach ($feed as $studioid => $item) {
					$studio = $sorm->get_one($studioid);
					if ($studio['airtime'] < config::$nowtime) {
						continue;
					}
					$studio['tags'] = empty($studio['tags'])? array():json_decode($studio['tags'], true);
					$anchor = $aorm->get_one($studio['userid']);
					$user   = $uorm->set_devision($studio['userid'])->get_one($studio['userid']);
					$lv     = api_lv::item($user['lv'], 0);
					$iscurated = api_studio::grade($anchor['starlv'], $studio['iscurated']);
					$user['nickname'] = htmlspecialchars_decode($user['nickname']);
					$list[] = array('userid'=>$studio['userid'], 'studioid'=>$studioid, 'nickname'=>$user['nickname'],
						'avtar'=>$user['thumb'], 'lvicon'=>$lv['iconsrc'], 'title'=>$studio['title'], 'thumb'=>$studio['thumb'],
						'viewer'=>$studio['viewer'], 'olstatus'=>$studio['olstatus'], 'airtime'=>$studio['airtime'], 'isfeed'=>1,
						'neartime'=>$studio['neartime'], 'alv'=>$user['lv'], 'tags'=>$studio['tags'], 'iscurated'=>$iscurated);
				}
			}
		}
		sview::$view->assign('datasets', $list);
		sview::$view->display();
	}
	
	//我的历史访问4-2-3
	private static function __dovisitor() {
		$acl    = acl::getinst('vedio');
		$userid = $acl->user['id'];
		$where  = array('userid'=>$userid);
		$field  = 'anchorid as userid, studioid, title, thumb, category as tags, stime';
		$list   = model::load('live/visitor')->set_devision($userid)->get_only_list(0, 10, $where, $field, 'stime', orm::DESC);
		if ($list) {
			$uorm = model::load('live/userinfo');
			$sorm = model::load('live/studio');
			$aorm = model::load('live/anchor');
			foreach ($list as &$item) {
				$user      = $uorm->set_devision($item['userid'])->get_one($item['userid']);
				$anchor    = $aorm->get_one($item['userid']);
				$studio    = $sorm->get_one($item['studioid']);
				$lv        = api_lv::item($user['lv']);
				$iscurated = api_studio::grade($anchor['starlv'], $studio['iscurated']);
				$item['lvicon']   = $lv['iconsrc'];
				$item['iscurated']= $iscurated;
				$item['avtar']    = $user['thumb'];
				$item['nickname'] = htmlspecialchars_decode($user['nickname']);
				$item['viewer']   = $studio['viewer'];
				$item['olstatus'] = $studio['olstatus'];
				$item['airtime']  = $studio['airtime'];
				$item['neartime'] = $studio['neartime'];
			}
		}
		sview::$view->assign('datasets', $list);
		sview::$view->display();
	}
}